#include "comm.hpp"
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
    //新建管道
    umask(0);
    int n = mkfifo(FIFO_FILE, 0666);
    if(n < 0)
    {
        std::cerr << "mkfifo" << std::endl;
        return 1;
    }

    //打开管道文件
    int fd = open(FIFO_FILE, O_RDONLY);
    if(fd < 0)
    {
        std::cerr << "open" << std::endl;
        return 1;
    }

    //读取信息
    while(true)
    {
        char buffer[1024] = {0};
        int num = read(fd, buffer, sizeof(buffer) - 1);
        buffer[num] = '\0';
        if(num > 0)
        {
            std::cout << "client say:" << buffer << std::endl;
        }
        else if(num == 0)
        {
            std::cout << "client退出" << std::endl;
            break;
        }
        else
        {
            std::cout << "读取错误" << std::endl;
            break;
        }
    }

    //关闭管道文件
    close(fd);

    //删除管道
    unlink(FIFO_FILE);
    return 0;
}